package com.uum.admin.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.uum.admin.pojo.entity.SysDictItem;
import com.uum.admin.pojo.query.DictItemPageQuery;
import com.uum.admin.service.SysDictItemService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import com.uum.common.core.valid.InsertGroup;
import com.uum.common.core.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "字典项接口")
@RestController
@RequestMapping("/api/v1/dict-items")
@RequiredArgsConstructor
public class SysDictItemController {

    private final SysDictItemService sysDictItemService;

    @Operation(description = "查询字典项列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation(value = "字典项列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", defaultValue = "1", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "10", value = "每页数量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "字典名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictCode", value = "字典编码", paramType = "query", dataType = "String")
    })
    @GetMapping("/page")
    public Result pageDictItem(@Validated DictItemPageQuery dictItemPageQuery) {
        IPage<SysDictItem> result = sysDictItemService.page(
                new Page<>(dictItemPageQuery.getCurrentPage(), dictItemPageQuery.getPageSize()),
                new LambdaQueryWrapper<SysDictItem>()
                        .eq(SysDictItem::getDictCode,dictItemPageQuery.getDictCode())
                        .like(StrUtil.isNotEmpty(dictItemPageQuery.getName()),SysDictItem::getName,dictItemPageQuery.getName())
                        .orderByAsc(SysDictItem::getSort));
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "字典项列表")
    @GetMapping("list")
    public Result listDictItem(String name, String dictCode) {
        List<SysDictItem> list = sysDictItemService.list(
                new LambdaQueryWrapper<SysDictItem>()
                        .like(StrUtil.isNotBlank(name), SysDictItem::getName, name)
                        .eq(StrUtil.isNotBlank(dictCode), SysDictItem::getDictCode, dictCode)
                        .select(SysDictItem::getName, SysDictItem::getValue)
                        .orderByAsc(SysDictItem::getSort)
        );
        return Result.success(list);
    }

    @ApiOperation(value = "字典项详情")
    @ApiImplicitParam(name = "id", value = "字典id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("detail/{id}")
    public Result DictItemDetail(@PathVariable Integer id) {
        SysDictItem dictItem = sysDictItemService.getById(id);
        return Result.success(dictItem);
    }

    @Operation(description = "新增字典项",businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增字典项")
    @ApiImplicitParam(name = "dictItem", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysDictItem")
    @PostMapping("add")
    public Result addDictItem(@Validated(InsertGroup.class) @RequestBody SysDictItem dictItem) {
        boolean status = sysDictItemService.save(dictItem);
        return Result.judge(status);
    }

    @Operation(description = "修改字典项",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改字典项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictItem", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysDictItem")
    })
    @PutMapping(value = "update")
    public Result updateDictItem(@Validated(UpdateGroup.class) @RequestBody SysDictItem dictItem) {
        boolean status = sysDictItemService.updateById(dictItem);
        return Result.judge(status);
    }

    @Operation(description = "删除字典项",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除字典项数据")
    @ApiImplicitParam(name = "ids", value = "主键ID集合，以,分割拼接字符串", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("delete/{ids}")
    public Result deleteDictItem(@PathVariable String ids) {
        boolean status = sysDictItemService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

}


